﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

using AbstractSql;

using MbUnit.Framework;

namespace UnitTests {
    [TestsOn(typeof(Reader))]
    [TestFixture]
    public class ReaderTests {
        public ReaderTests() {
            AbsDBConnection.SetDefaultConnectionString("Test");
        }
        [Test]
        public void CanReadSingleIntValue() {
            int iDummy = -1;
            AbsDBCommand
                .SqlCommand("select top 1 ColInt from test where ColInt = 3")
                .ExecuteReader(delegate(IDataReader r) {
                iDummy = Reader.Read<int>(r, "ColInt");
            });
            Assert.AreEqual(3, iDummy);
        }
        [Test]
        public void CanReadSingleString() {
            string sDummy=null;
            AbsDBCommand
                .SqlCommand("select top 1 ColString from test where ColString='test'")
                .ExecuteReader(delegate(IDataReader r) {
                sDummy = Reader.Read<string>(r, "ColString");
            });
            Assert.AreEqual("test", sDummy);
        }
        [Test]
        public void CanReadNonNullValues() {
            int iDummy=-1;
            string sDummy;
            DateTime dtDummy;
            AbsDBCommand
                .SqlCommand("select ColInt, ColString, ColDate from test where ColInt is not null")
                .ExecuteReader(delegate(IDataReader r) {
                iDummy = Reader.Read<int>(r, "ColInt");
                sDummy = Reader.Read<string>(r, "ColString");
                dtDummy = Reader.Read<DateTime>(r, "ColDate");
            });
            Assert.AreNotEqual(-1, iDummy);
        }
        [Test]
        public void CanReadNullableValues() {
            int? iDummy=0;
            string sDummy=null;
            DateTime? dtDummy=null;
            AbsDBCommand
                .SqlCommand("select ColInt, ColString, ColDate from test where ColInt is null")
                .ExecuteReader(delegate(IDataReader r) {
                iDummy = Reader.Read<int?>(r, "ColInt");
                sDummy = Reader.Read<string>(r, "ColString");
                dtDummy = Reader.Read<DateTime?>(r, "ColDate");
            });
            Assert.IsNull(iDummy);
        }
    }
}